package com.personal.book.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.personal.book.entity.OrdersDetails;
import com.personal.book.entity.Users;
import com.personal.book.service.impl.OrdersDetailsServiceImpl;
import com.personal.tools.PageData;

/**
 * Servlet implementation class OrdersDetailsServlet
 */
@WebServlet("/OrdersDetailsServlet")
public class OrdersDetailsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrdersDetailsServiceImpl odsi = new OrdersDetailsServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrdersDetailsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 编码
		request.setCharacterEncoding("utf-8");
		// 标记当前为何种操作的变量
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("queryByPage".equals(op)) {
			doQueryByPage(request, response);
		}else if("query".equals(op)) {
			doQuery(request,response);
		}else if("search".equals(op)) {
			doSearch(request,response);
		}
	}

	/**
	 * 管理员查询订单详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int orderId = 1;
		// 2获取请求参数
		if (request.getParameter("orderId") != null) {
			orderId = Integer.parseInt(request.getParameter("orderId"));
		}
		// 页码默认值为1
		int page = 1;
		// 每页记录数默认为10
		int pageSize = 2;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 3 调用 Service的方法
		PageData<OrdersDetails> pd = odsi.getOrdersDetailsById(page, pageSize, orderId);

		// 4传递PageData对象给jsp，此时传递的是pageData的对象
		request.setAttribute("pd", pd);

		// 4.-1转发回来的时候，我们将查询关键字也存储在request范围内
		request.setAttribute("orderId", orderId);

		// 5 转发
		request.getRequestDispatcher("adminOrdersDetails.jsp").forward(request, response);
	}
	
	
	/**
	 * 用户查询订单详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int orderId = 1;
		// 2获取请求参数
		if (request.getParameter("orderId") != null) {
			orderId = Integer.parseInt(request.getParameter("orderId"));
		}
		
		// 3 调用 Service的方法
		List<OrdersDetails> list = odsi.getOrdersDetalsById(orderId);

		// 4传递PageData对象给jsp，此时传递的是pageData的对象
		request.setAttribute("list", list);

		// 4.-1转发回来的时候，我们将查询关键字也存储在request范围内
		request.setAttribute("orderId", orderId);

		// 5 转发
		request.getRequestDispatcher("ordersdetails.jsp").forward(request, response);
	}
	
	/**
	 * 用户查询订单详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doSearch(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keys = null;
		// 2获取请求参数
		if (request.getParameter("search") != null) {
			keys = request.getParameter("search");
		}
		
		// 3 调用 Service的方法
		List<OrdersDetails> list = odsi.getOrdersDetalsByName(keys);

		// 4传递PageData对象给jsp，此时传递的是pageData的对象
		request.setAttribute("list", list);

		
		// 5 转发
		request.getRequestDispatcher("ordersdetails.jsp").forward(request, response);
	}
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
